package com.yunhe.option.repository.data.impl;

import com.yunhe.common.util.StringUtil;
import com.yunhe.option.domain.data.GeneratePrice;
import org.apache.commons.lang3.StringUtils;
import org.neo4j.ogm.model.Result;
import org.neo4j.ogm.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * 发电电价仓库自定义实现
 */
@Repository
public class GeneratePriceDao {
    @Autowired
    private Session session;

    /**
     * 执行语句查找发电电价
     * @param cypher 执行语句
     */
    public List<Map<String, Object>> search(String cypher){
        Result result = session.query(cypher, new HashMap<>());
        Iterator<Map<String, Object>> iterator = result.iterator();
        List<Map<String, Object>> list = new ArrayList<>();
        while (iterator.hasNext()){
            list.add(iterator.next());
        }
        return list;
    }

    /**
     * 模糊查询条件
     * @return
     */
    private String getQuery(){
//        return "match (gp:GeneratePrice)-[:AREA_OF]->(d:Area), p=(n)-[r:HAS_CHILD*..2]->(d)"
//                + " where gp.title contains {queryStr} or gp.property contains {queryStr}"
//                + " or toString(gp.windPrice) contains {queryStr} or toString(gp.pvPrice) contains {queryStr}"
//                + " d.title contains {queryStr} or n.title contains {queryStr}";
        return " where gp.title contains {queryStr}"
                + " or gp.province contains {queryStr} or gp.city contains {queryStr} or gp.district contains {queryStr}"
                + " or toString(gp.windPrice) contains {queryStr} or toString(gp.pvPrice) contains {queryStr}";
    }

    /**
     * 得到排序规则
     * @param sortList 排序字段列表
     * @return
     */
    private String getSort(List<String> sortList){
        String sort = "order by";
        for (String s : sortList) {
            if (s.startsWith("-")) {
                s = s.replace("-", "");
                sort += "-s." + s + ",";
            } else {
                sort += "s." + s + ",";
            }
        }
        sort = sort.substring(0, sort.length() - 1);
        return sort;
    }

    /**
     * 得到分页条件
     * @param page 页数
     * @param size 每页条数
     * @return
     */
    private String getPage(int page, int size) {
        return " skip" + page * size + " limit " + size;
    }

    public Iterable<GeneratePrice> getByQueryStr(String queryStr,
                                                 int page,
                                                 int size,
                                                 List<String> sortList){
        Map<String, Object> map = new HashMap<>();
        String cql = "match (gp:GeneratePrice) ";
        if(StringUtils.isNotEmpty(queryStr)){
            map.put("queryStr", queryStr);
            String query = getQuery();
            cql += query;
        }
        if(sortList != null){
            String sort = getSort(sortList);
            cql += sort;
        }
        if (page > 0 && size > 0) {
            String pageStr = getPage(page, size);
            cql += pageStr;
        }
        cql += " return gp";
        return session.query(GeneratePrice.class, cql, map);
    }

    public Iterable<GeneratePrice> getInfo(List<Long> idList){
        Map<String, Object> map = new HashMap<>();
        map.put("idList", idList);
        String cql = "match p=(gp:GeneratePrice)-[:AREA_OF]->(a:Area) where ID(gp) in {idList} return p";
//        String cql = "match p=(gp:GeneratePrice) where ID(gp) in {idList} return p";
        return session.query(GeneratePrice.class, cql, map);
    }
}
